﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Xml.Utility;

namespace Portal.Cms.Model
{
    public class GroupDataCls
    {
        public string GroupDataId;
        public string frkDataTypeId;
        public string DataTypeName;
        public string GroupDataName;
        public string GroupDataDescription;
        public string Alias;
        
        public string Avatar;
        public DateTime EntryDate;
        public string LinkDataAlias;
        public int AllowSub=0;
        public int AllowHtmlBody = 0;
        public int ActiveGroupData = 0;
        public string AssetCode = "";
        public string PlugInModuleId;

        public string GetAvatarUrl(string SiteId,string HttpDataRoot)
        {
            return HttpDataRoot + "/Cms/" + AssetCode + "/" + GroupDataId + "/" + Avatar;
        }
        public string GetSmallAvatarUrl(string SiteId, string HttpDataRoot)
        {
            return HttpDataRoot + "/Cms/" + AssetCode + "/" + GroupDataId + "/thumb/" + Avatar;
        }

        public bool ShowBody
        {
            get
            {
                if (AllowHtmlBody==1)// frkDataTypeId.Trim().ToLower().Equals("html") || frkDataTypeId.Trim().ToLower().Equals("news"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }

    public class GroupDataParser
    {
        public static GroupDataCls CreateInstance()
        {
            //tao 1 class trang....
            GroupDataCls oGroupDataCls = new GroupDataCls();
            return oGroupDataCls;
        }

        public static GroupDataCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            GroupDataCls oGroupDataCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oGroupDataCls;
        }


        public static GroupDataCls ParseFromDataRow(DataRow dr)
        {
            GroupDataCls oGroupDataCls = new GroupDataCls();
            oGroupDataCls.GroupDataId = XmlUtility.GetString(dr, "GroupDataId", true);
            oGroupDataCls.frkDataTypeId = XmlUtility.GetString(dr, "frkDataTypeId", true);
            oGroupDataCls.DataTypeName = XmlUtility.GetString(dr, "DataTypeName", true);
            oGroupDataCls.GroupDataName = XmlUtility.GetString(dr, "GroupDataName", true);
            oGroupDataCls.GroupDataDescription = XmlUtility.GetString(dr, "GroupDataDescription", true);
            oGroupDataCls.EntryDate = XmlUtility.GetDate(dr, "EntryDate", true);
            oGroupDataCls.Alias = XmlUtility.GetString(dr, "Alias", true);
            oGroupDataCls.LinkDataAlias = XmlUtility.GetString(dr, "LinkDataAlias", true);
            oGroupDataCls.AllowSub = XmlUtility.GetInt(dr, "AllowSub", true);
            oGroupDataCls.AllowHtmlBody = XmlUtility.GetInt(dr, "AllowHtmlBody", true);
            oGroupDataCls.Avatar = XmlUtility.GetString(dr, "Avatar", true);
            oGroupDataCls.AssetCode = XmlUtility.GetString(dr, "AssetCode", true);
            oGroupDataCls.PlugInModuleId = XmlUtility.GetString(dr, "PlugInModuleId", true);
            oGroupDataCls.ActiveGroupData = XmlUtility.GetInt(dr, "ActiveGroupData", true);
            return oGroupDataCls;
        }

        public static GroupDataCls[] ParseFromDataTable(DataTable dtTable)
        {
            GroupDataCls[] GroupDatas = new GroupDataCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                GroupDatas[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return GroupDatas;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Info");

            ds.Tables["Info"].Columns.Add("GroupDataId");
            ds.Tables["Info"].Columns.Add("frkDataTypeId");
            ds.Tables["Info"].Columns.Add("DataTypeName");
            ds.Tables["Info"].Columns.Add("GroupDataName");
            ds.Tables["Info"].Columns.Add("GroupDataDescription");
            ds.Tables["Info"].Columns.Add("Alias");
            ds.Tables["Info"].Columns.Add("EntryDate");
            ds.Tables["Info"].Columns.Add("LinkDataAlias");
            ds.Tables["Info"].Columns.Add("AllowSub");
            ds.Tables["Info"].Columns.Add("AllowHtmlBody");
            ds.Tables["Info"].Columns.Add("Avatar");
            ds.Tables["Info"].Columns.Add("AssetCode");
            ds.Tables["Info"].Columns.Add("ActiveGroupData");
            ds.Tables["Info"].Columns.Add("PlugInModuleId");
            return ds;
        }

        public static string GetXml(GroupDataCls oGroupDataCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                oGroupDataCls.GroupDataId,
                oGroupDataCls.frkDataTypeId,
                oGroupDataCls.DataTypeName,
                oGroupDataCls.GroupDataName,
                oGroupDataCls.GroupDataDescription,
                oGroupDataCls.Alias,
                oGroupDataCls.EntryDate,
                oGroupDataCls.LinkDataAlias,
                oGroupDataCls.AllowSub,
                oGroupDataCls.AllowHtmlBody,
                oGroupDataCls.Avatar,
                oGroupDataCls.AssetCode,
                oGroupDataCls.ActiveGroupData,
                oGroupDataCls.PlugInModuleId,
            });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            GroupDataCls oGroupData = GroupDataParser.ParseFromDataRow(dr);
            string Xml = GroupDataParser.GetXml(oGroupData);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            GroupDataCls[] GroupDatas = new GroupDataCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                GroupDatas[iIndex] = GroupDataParser.ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = GroupDataParser.GetXml(GroupDatas);
            return Xml;
        }


        public static string GetXml(GroupDataCls[] GroupDatas)
        {
            if (GroupDatas == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                GroupDatas = new GroupDataCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < GroupDatas.Length; iIndex++)
            {
                GroupDataCls oGroupDataCls = GroupDatas[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                {
                    oGroupDataCls.GroupDataId,
                    oGroupDataCls.frkDataTypeId,
                    oGroupDataCls.DataTypeName,
                    oGroupDataCls.GroupDataName,
                    oGroupDataCls.GroupDataDescription,
                    oGroupDataCls.Alias,
                    oGroupDataCls.EntryDate,
                    oGroupDataCls.LinkDataAlias,
                    oGroupDataCls.AllowSub,
                    oGroupDataCls.AllowHtmlBody,
                    oGroupDataCls.Avatar,
                    oGroupDataCls.AssetCode,
                    oGroupDataCls.ActiveGroupData,
                    oGroupDataCls.PlugInModuleId
                });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static GroupDataCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new GroupDataCls[0];
            }
            GroupDataCls[] GroupDatas = new GroupDataCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                GroupDatas[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return GroupDatas;
        }
    }
}
